package com.neusoft.shopping.manager.controller;

import com.neusoft.shopping.manager.entity.GoodsEntity;
import com.neusoft.shopping.manager.entity.PictureEntity;
import com.neusoft.shopping.manager.service.GoodsService;
import com.neusoft.shopping.manager.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class PictureController {
    @Autowired
    private PictureService service;

    @RequestMapping("/picture_show")
    public ModelAndView getPicture(){
        ModelAndView modelAndView=new ModelAndView();
        List<PictureEntity> pictures=service.qureyAll();
        modelAndView.addObject("pictures",pictures);
        modelAndView.setViewName("picture_show");
        return modelAndView;
    }
    @RequestMapping("/dofile")
    public String file_UpLoad(@RequestParam("ufile") MultipartFile multipartFile
           ) throws IOException {
        String sfname = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf("."));
        String newName = UUID.randomUUID().toString().replace("-","X");
        File newfile = new File("/D/images/picture/"+newName+sfname);
        multipartFile.transferTo(newfile);
        return "forward:picture_show.do";
    }
}
